package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.activity.MedicalDistrictActivity.ViewHolder;
import com.health.work.model.ProductsBean;
import com.health.work.utils.AsyncImageLoader;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Products extends Activity {

	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private static final int NODATA = 3;
	private String mobilePhoneNum;
	private Thread thread;
	private String BusinessTypeId;
	private  ArrayList<ProductsBean>proList;
	private GridView grid;
	private Button back;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				proAdapter adapter=new proAdapter(Products.this);
				grid.setAdapter(adapter);
				break;
			case FAILE:
				TipsUtil.showAlert(Products.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(Products.this, null, "未知错误！");
				break;
			case NODATA:
				TipsUtil.showAlert(Products.this, null, "暂无数据！");
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.products_layout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPages();
		getData();
	}

	private void initPages() {
		// TODO Auto-generated method stub
		grid=(GridView)findViewById(R.id.brand_grid);
		back=(Button)findViewById(R.id.btnBack);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			BusinessTypeId = (String) bundle.getString("BusinessTypeId");
		}
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				Intent intent;
				intent=new Intent(Products.this, ProductDetailsActivity.class);
				intent.putExtra("Name", proList.get(arg2).Name);//名称
				intent.putExtra("Photo", proList.get(arg2).Photo);//照片
				intent.putExtra("Price", proList.get(arg2).Price+"");//价钱
				intent.putExtra("Quality", proList.get(arg2).Quality);//品质
				intent.putExtra("GoodCount", proList.get(arg2).GoodCount+"");
				intent.putExtra("PreOrder", proList.get(arg2).PreOrder+"");
				intent.putExtra("Id", proList.get(arg2).Id+"");
				startActivity(intent);
			}
		});
	}

	private void getData() {
		// TODO Auto-generated method stub
		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().SelectProduct("", "", "", "",
						BusinessTypeId, "", "", new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if("0".equals(result)){
									handler.sendEmptyMessage(NODATA);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("2", array.toString());
								if(array.toString()!=null&&array.length()>0){
									proList=new ArrayList<ProductsBean>();
									for (int i = 0; i < array.length(); i++) {
										ProductsBean bean=new ProductsBean();
										bean.AttentionNum=array.getJSONObject(i).getInt("AttentionNum");
										bean.GoodCount=array.getJSONObject(i).getInt("GoodCount");
										bean.Id=array.getJSONObject(i).getInt("Id");
										bean.MobilePhone=array.getJSONObject(i).getString("MobilePhone");
										bean.Name=array.getJSONObject(i).getString("Name");
										bean.Photo=array.getJSONObject(i).getString("Photo");
										bean.PreOrder=array.getJSONObject(i).getInt("PreOrder");
										bean.Price=array.getJSONObject(i).getInt("Price");
										bean.Quality=array.getJSONObject(i).getString("Quality");
										proList.add(bean);
									}
									handler.sendEmptyMessage(SUCCESS);
								}
							}
						});
			}
		});
		thread.start();
	}

	private class proAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private AsyncImageLoader asyncImageLoader;
		Activity context;

		public proAdapter(Context context) {
			this.context = (Activity) context;
			asyncImageLoader = new AsyncImageLoader();
			inflater = this.context.getLayoutInflater();
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return proList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return proList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHodler;
			if (convertView == null) {
				viewHodler = new ViewHolder();
				convertView = inflater.inflate(R.layout.grid_item,
						parent, false);
				viewHodler.headImageView=(ImageView)convertView.findViewById(R.id.brand_grid_Image);
				viewHodler.nameTextView=(TextView)convertView.findViewById(R.id.brand_grid_Text);
			} else {
				viewHodler = (ViewHolder) convertView.getTag();
			}
			ProductsBean bean=proList.get(position);
			if (bean.Photo != null) {
				Drawable drawable = asyncImageLoader
						.loadImageFromUrl(bean.Photo);
				if (drawable != null) {
					viewHodler.headImageView.setImageDrawable(drawable);
				} else {
					viewHodler.headImageView
							.setImageResource(R.drawable.nophoto);
				}
			}
			viewHodler.nameTextView.setText(bean.Name);
			convertView.setTag(viewHodler);
			return convertView;
		}

	}
	class ViewHolder {
		public ImageView headImageView;
		public TextView nameTextView;
	}
}
